#include <stdio.h>
#include <tchar.h>
#include "ocilib.h"

void err_handler(OCI_Error *err)
{
    printf(
            "code  : ORA-%05i\n"
            "msg   : %s\n"
            "sql   : %s\n",
            OCI_ErrorGetOCICode(err),
            OCI_ErrorGetString(err),
            OCI_GetSql(OCI_ErrorGetStatement(err))
    );
}

int main(int argc, _TCHAR* argv[])
{

    OCI_Connection* cn;
    OCI_Statement* st;
    OCI_Resultset* rs;


    OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT);

    cn  = OCI_ConnectionCreate("127.0.0.1:1521/ORCL", "scott", "tiger", OCI_SESSION_DEFAULT);
     printf("%d\n",cn==NULL);
    if(cn == NULL){
        err_handler(OCI_GetLastError());
        printf("%i",OCI_GetVersionServer(cn));
        printf("连接失败！\n");
    }
    st  = OCI_StatementCreate(cn);

    OCI_ExecuteStmt(st, "select empno,ename,sal,deptno from emp");

    rs = OCI_GetResultset(st);

    while (OCI_FetchNext(rs)){
        printf("code: %i, name %s,salary %d,department# %d\n", OCI_GetInt(rs, 1)  , OCI_GetString(rs, 2),OCI_GetInt(rs, 3),OCI_GetInt(rs, 4));
    }

    printf("\n%d row(s) fetched\n", OCI_GetRowCount(rs));


    OCI_Cleanup();


    char a[20];
    gets(a);

    return EXIT_SUCCESS;
}

